package com.mes.cloud.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.mes.cloud.dao.BmeBcDAO;
import com.mes.cloud.dao.BmeBcPickmapDAO;
import com.mes.cloud.dto.BcViewDTO;
import com.mes.cloud.dto.BmeBcDTO;
import com.mes.cloud.dto.BmeBcPickmapDTO;
import com.mes.cloud.service.BmeBcPickmapService;
import com.mes.cloud.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.github.pagehelper.page.PageMethod.startPage;

/**
* bme_bc_pickmap基础表
*
* @author  xhy
* @date    2019/05/09 16:00
* @version 1.0
*/
@Service
public class BmeBcPickmapServiceImpl implements BmeBcPickmapService {
    @Autowired
    private BmeBcPickmapDAO bmeBcPickmapDAO;

    @Autowired
    private BmeBcDAO bmeBcDAO;

    /**
    * 根据条件获取bme_bc_pickmap基础表列表
    *
    * @author xhy
    * @date   2019/05/09 16:00
    * @param  condition
    * @return List<BmeBcPickmapDTO>
    */
    @Override
    public List<BmeBcPickmapDTO> getBmeBcPickmapList(Map<Object, Object> condition) {
        return bmeBcPickmapDAO.getBmeBcPickmapList(condition);
    }

    /**
    * 获取bme_bc_pickmap基础表的最大记录数
    *
    * @author xhy
    * @date   2019/05/09 16:00
    * @param  condition
    * @return long
    */
    @Override
    public long getBmeBcPickmapCount(Map<Object, Object> condition) {
        return bmeBcPickmapDAO.getBmeBcPickmapCount(condition);
    }

    /**
    * 新增bme_bc_pickmap基础表
    *
    * @author xhy
    * @date   2019/05/09 16:00
    * @param  bmeBcPickmapDTO
    * @return long
    */
    @Override
    public long insertBmeBcPickmap(BmeBcPickmapDTO bmeBcPickmapDTO) {
        bmeBcPickmapDTO.setId(UuId.getShortUuid());
        return bmeBcPickmapDAO.insertBmeBcPickmap(bmeBcPickmapDTO);
    }

    /**
    * 更新bme_bc_pickmap基础表
    *
    * @author xhy
    * @date   2019/05/09 16:00
    * @param  bmeBcPickmapDTO
    * @return long
    */
    @Override
    public long updateBmeBcPickmap(BmeBcPickmapDTO bmeBcPickmapDTO) {
        return bmeBcPickmapDAO.updateBmeBcPickmap(bmeBcPickmapDTO);
    }

    /**
    * 删除bme_bc_pickmap基础表
    *
    * @author xhy
    * @date   2019/05/09 16:00
    * @param  id
    * @return long
    */
    @Override
    public long delBmeBcPickmapById(String id) {
        return bmeBcPickmapDAO.delBmeBcPickmapById(id);
    }

    /**
    * 根据bme_bc_pickmap基础表标识获取bme_bc_pickmap基础表
    *
    * @author xhy
    * @date   2019/05/09 16:00
    * @param  id
    * @return long
    */
    @Override
    public BmeBcPickmapDTO getBmeBcPickmapById(String id) {
        return bmeBcPickmapDAO.getBmeBcPickmapById(id);
    }

    @Override
    public CommPager<BmeBcPickmapDTO> findPageList(Map<Object, Object> condition) {
        int pageIndex = condition.get("pageIndex") == null ? 0 : Integer.valueOf(condition.get("pageIndex").toString());
        int pageSize = condition.get("pageSize") == null ? 10 : Integer.valueOf(condition.get("pageSize").toString());
        Page<BmeBcPickmapDTO> page = PageHelper.startPage(pageIndex, pageSize, true);
        List<BmeBcPickmapDTO> list = bmeBcPickmapDAO.getBmeBcPickmapList(condition);
        CommPager<BmeBcPickmapDTO> pageResult = new CommPager<>(page, list);
        return pageResult;
    }

    /**
     * 批量删除
     *
     * @param map
     * @return
     */
    @Override
    public Long batchDelete(Map<Object, Object> map) throws Exception {
        if (map.get("ids") == null ) {
            throw new Exception("ids不能为空");
        }
        return bmeBcPickmapDAO.batchDelete((List<String>) map.get("ids"));
    }
    @Override
    public long enable(Map<Object, Object> map) throws Exception   {
        if(map.get("id") == null || map.get("isEnable") == null){
            throw new Exception("参数异常");
        }
        return bmeBcPickmapDAO.enable(map);
    }

    @Override
    public Long batchEnable(Map<Object, Object> map) throws Exception {
        if (map.get("ids") == null) {
            throw new Exception("ids不能为空");
        }
        return bmeBcPickmapDAO.batchEnable((List<Long>) map.get("ids"));
    }

    @Override
    public List<BmeBcPickmapDTO>  findListByCode(Map<Object, Object> map) throws Exception {
        if(map.get("code") == null ){
            throw new Exception("参数异常");
        }
        BmeBcDTO bmeBcDTO = bmeBcDAO.getBmeBcByCode(map.get("code").toString());
        Map<Object, Object> condition = new HashMap<>();
        condition.put("pid",bmeBcDTO.getId());
        return bmeBcPickmapDAO.getBmeBcPickmapList(condition);
    }

    @Override
    public int importExcel(MultipartFile file) throws Exception{
        String rooPath = this.getClass().getResource("/").getPath();
        //复制文件到static目录
        String path = rooPath + "/static";
        String fileName = FileUploadUtils.uploadFile(file, path);
        String filePath = FileUploadUtils.uploadFile(file, path, fileName);
        FileInputStream fis = null;
        fis = new FileInputStream(filePath);
        // 创建工具类.
        ExcelUtil<BmeBcPickmapDTO> util = new ExcelUtil<BmeBcPickmapDTO>(BmeBcPickmapDTO.class);
        List<BmeBcPickmapDTO> paramList = util.importExcel(fileName, fis);
        HashMap<Object, Object> objectObjectHashMap = Maps.newHashMap();
        if (StringUtils.isEmpty(paramList)){
            throw new Exception("数据为空！");
        }
        for (BmeBcPickmapDTO bmeBcPickmapDTO : paramList){
            if (StringUtils.isEmpty(bmeBcPickmapDTO.getId())){
                bmeBcPickmapDTO.setId(UuId.getShortUuid());
            }else {
                objectObjectHashMap.put("id",bmeBcPickmapDTO.getId());
                long count = bmeBcPickmapDAO.getBmeBcPickmapCount(objectObjectHashMap);
                if (count>0){
                    throw new Exception("数据已经存在！");
                }
            }
        }
        return bmeBcPickmapDAO.insertExcel(paramList);
    }

    @Override
    public void exportFile(Map<Object, Object> map, HttpServletResponse response) throws Exception {
        List<BmeBcPickmapDTO> list = bmeBcPickmapDAO.getBmeBcPickmapList(map);
        if (list.size() == 0) {
            throw new Exception("导出数据为空！");
        }
        // 创建工具类
        ExcelUtil<BmeBcPickmapDTO> util = new ExcelUtil<BmeBcPickmapDTO>(BmeBcPickmapDTO.class);
        // 导出
        String topName = DateUtil.getDateTime("yyyyMMddHHmmss");
        util.exportFile(list, DateUtil.getDateTime(topName), topName, response);
    }
}
